package com.camera2.http;

import android.content.Context;
import android.util.Log;
import com.camera2.Utils.StreamUtils;
import com.camera2.bean.ApplicationHelper;
import com.camera2.bean.MessageBean;
import com.camera2.local.SharedPrefer;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ExpertSendMessage {
	public static String voicePath;
	private String TAG="SendImage";
	static String message;
	static String token;
	public static String send1(Context context) throws Exception {


		token= SharedPrefer.ReadToken(context);
		String expertId=SharedPrefer.Readid(context);
		MessageBean bean = ((ApplicationHelper) context.getApplicationContext().getApplicationContext()).getMessageBean();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
		Date date = new Date(System.currentTimeMillis());
		OkHttpClient client = new OkHttpClient();

		MultipartBody.Builder builder = new MultipartBody.Builder()

				.addFormDataPart("id", bean.getId()+"")
				.addFormDataPart("expertId", expertId)
				.addFormDataPart("userId", bean.getUserId()+"")
				.addFormDataPart("userMessage", bean.getUserMessage())
				.addFormDataPart("expertMessage", bean.getExpertMessage())
				.addFormDataPart("userVoice", bean.getUserVoice()+"")
				.addFormDataPart("expertVoice", bean.getExpertVoice()+"")
				.addFormDataPart("userCreateTime", bean.getUserCreateTime())
				.addFormDataPart("expertCreateTime", simpleDateFormat.format(date));


		MultipartBody requestBody = builder.build();
		Request request = new Request.Builder()
				.url(GlobalUrl.getMessageUpdateUrl())
				.post(requestBody)
				.build();
		Response response = client.newCall(request).execute();
		String result = response.body().string();
		System.out.println("图片结果" + result);
		JSONObject jsonObj = new JSONObject(result);
		message = jsonObj.getString("msg");

		return message;
	}
	public static String send(Context context)
			throws Exception {
		String token = SharedPrefer.ReadToken(context);
		String expertId=SharedPrefer.Readid(context);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
		Date date = new Date(System.currentTimeMillis());

		MessageBean bean = ((ApplicationHelper) context.getApplicationContext().getApplicationContext()).getMessageBean();
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(GlobalUrl.getMessageUpdateUrl());
		httpPost.setHeader("token", token);
		httpPost.addHeader("Content-Type", "application/json");
		JSONObject jsonParam = new JSONObject();

		jsonParam.put("id", bean.getId() + "");
		jsonParam.put("expertId", expertId);
		jsonParam.put("userId", bean.getUserId() + "");
		jsonParam.put("userMessage", bean.getUserMessage());
		jsonParam.put("expertMessage", bean.getExpertMessage());
		jsonParam.put("userVoice", bean.getUserVoice()+"");
		jsonParam.put("expertVoice", bean.getExpertVoice());
		jsonParam.put("userCreateTime", bean.getUserCreateTime());
		jsonParam.put("expertCreateTime", simpleDateFormat.format(date));


		StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");//解决中文乱码问题
		entity.setContentEncoding("UTF-8");
		entity.setContentType("application/json");
		httpPost.setEntity(entity);

		HttpResponse response = client.execute(httpPost);
		int code = response.getStatusLine().getStatusCode();
		InputStream is = response.getEntity().getContent();
		String result = StreamUtils.readStream(is);
		Log.i("UpdatePassword", "UpdatePassword: " + result);
		JSONObject jsonObj = new JSONObject(result);
		String msg = jsonObj.getString("msg");

		return msg;
	}
}
